package cn.itrip.search.service.impl;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.vo.ItripHotelVO;
import cn.itrip.beans.vo.SearchHotelVO;
import cn.itrip.common.Page;
import cn.itrip.search.service.SearchHotleService;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;

import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("searchHotleService")
public class SearcjHotleServiceImpl implements SearchHotleService {
    @Autowired
    private HttpSolrServer httpSolrServer;

    /**
     * 查询酒店分页
     *
     * @param vo
     * @return
     */
    @Override
    public Dto searchItripHotelPage(SearchHotelVO vo) {
        int pagno = 0;
        int size = 0;
        Dto dto = null;
        int temp = 0;
        SolrQuery solrQuery = new SolrQuery("*:*");
        StringBuffer bufferquery = new StringBuffer();
        if (vo != null) {
            if (vo.getDestination() != null && vo.getDestination() != "") {
                bufferquery.append(" destination :" + vo.getDestination());
                temp = 1;
            }
            if (vo.getHotelLevel() != null) {
                solrQuery.addFilterQuery("hotelLevel:" + vo.getHotelLevel() + "");
            }
            if (vo.getKeywords() != null && vo.getKeywords() != "") {
                if (temp == 1) {
                    bufferquery.append(" AND keyword :" + vo.getKeywords());
                } else {
                    bufferquery.append(" keyword :" + vo.getKeywords());
                }
            }
            if (vo.getFeatureIds() != null && vo.getFeatureIds() != "") {
                StringBuffer buffer = new StringBuffer("(");
                int flag = 0;
                String featureIdArray[] = vo.getFeatureIds().split(",");
                for (String featureId : featureIdArray) {
                    if (flag == 0) {
                        buffer.append(" featureIds:" + "*," + featureId + ",*");
                    } else {
                        buffer.append(" OR featureIds:" + "*," + featureId + ",*");
                    }
                    flag++;
                }
                buffer.append(")");
                solrQuery.addFilterQuery(buffer.toString());
            }
            if (vo.getTradeAreaIds() != null && vo.getTradeAreaIds() != "") {
                StringBuffer buffer = new StringBuffer("(");
                int flag = 0;
                String tradeAreaIdArray[] = vo.getTradeAreaIds().split(",");
                for (String tradeAreaId : tradeAreaIdArray) {
                    if (flag == 0) {
                        buffer.append(" tradingAreaIds:" + "*," + tradeAreaId + ",*");
                    } else {
                        buffer.append(" OR tradingAreaIds:" + "*," + tradeAreaId + ",*");
                    }
                    flag++;
                }
                buffer.append(")");
                solrQuery.addFilterQuery(buffer.toString());
            }
            if (vo.getMaxPrice() != null) {
                solrQuery.addFilterQuery("minPrice:" + "[* TO " + vo.getMaxPrice() + "]");
            }
            if (vo.getMinPrice() != null) {
                solrQuery.addFilterQuery("minPrice:" + "[" + vo.getMinPrice() + " TO *]");
            }

            if (vo.getAscSort() != "" && vo.getAscSort() != null) {
                solrQuery.addSort(vo.getAscSort(), SolrQuery.ORDER.asc);
            }

            if (vo.getDescSort() != null && vo.getDescSort() != "") {
                solrQuery.addSort(vo.getDescSort(), SolrQuery.ORDER.desc);
            }

        }
        if (bufferquery.toString() != null && bufferquery.toString() != "") {
            solrQuery.setQuery(bufferquery.toString());
        }
        if (vo.getPageNo() == null) {
            pagno = 1;
        }
        if (vo.getPageNo() != null) {
            pagno = vo.getPageNo();
        }
        if (vo.getPageSize() != null) {
            size = vo.getPageSize();
        }
        try {
            dto = new Dto();
            Integer rows = 5;
            Integer currPage = pagno - 1;
            Integer start = currPage * rows;
            solrQuery.setStart(start);
            //一页显示多少条
            solrQuery.setRows(rows);
            QueryResponse queryResponse = httpSolrServer.query(solrQuery);
            SolrDocumentList docs = queryResponse.getResults();
            Page<ItripHotelVO> page = new Page(currPage + 1, solrQuery.getRows(), new Long(docs.getNumFound()).intValue());
            List<ItripHotelVO> list = queryResponse.getBeans(ItripHotelVO.class);
            page.setRows(list);
            dto.setSuccess("true");
            dto.setMsg("列表显示成功");
            dto.setErrorCode("0");
            dto.setData(page);
        } catch (SolrServerException e) {
            dto = new Dto();
            dto.setSuccess("false");
            dto.setMsg("列表显示失败");
            e.printStackTrace();
        }
        return dto;
    }

    /***
     * 根据热门城市查询酒店
     * @param cityId
     * @param count
     * @return
     * @throws Exception
     */
    @Override
    public Dto searchItripHotelListByHotCity(Integer cityId, Integer count) {
        Dto dto = null;
        SolrQuery solrQuery = new SolrQuery("*:*");
        if (cityId != null && !cityId.equals("")) {
            solrQuery.addFilterQuery("cityId:" + cityId);
        } else {
            dto = new Dto();
            dto.setSuccess("false");
            dto.setMsg("查找失败");
            return null;
        }
        //设置起始页数
        solrQuery.setStart(0);
        //一页显示多少条
        if (count != null) {
            solrQuery.setRows(count);
        }
        QueryResponse queryResponse = null;
        try {
            queryResponse = httpSolrServer.query(solrQuery);
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
        List<ItripHotelVO> list = queryResponse.getBeans(ItripHotelVO.class);
        dto = new Dto();
        dto.setSuccess("true");
        dto.setData(list);
        dto.setMsg("显示成功");
        return dto;
    }
}
